package org.goochasca

private[goochasca] trait Legend extends Chart {
  
  private var dataLabels: Iterable[String] = None
  private var position: Option[(Position, Orientation)] = None
  
  def setLegend(position: Position, orientation: Orientation, labels: String*) {
    dataLabels = labels
    this.position = Some(position, orientation)
  }

  def setLegend(labels: String*) {
    dataLabels = labels
    this.position = None
  }

  import Chart._
  protected abstract override def appendParms(sb: StringBuilder) {
    super.appendParms(sb)
    var first = true
    for (label <- dataLabels) {
      if (first) {
        sb append "&chdl="
        first = false
      } else {
        sb append '|'
      } 
      sb append escape(label)
    }
    if (position.isDefined) {
      val pos = position.get
      sb append "&chdlp=" append pos._1.asLegend(pos._2)
    }
  }
}
